#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpf_inspects_base.py 
@version:
@time: 2022/04/18 
"""
from TapClientCore.T05_TM_feature.v2.TMF01_datasource.tmf_http import TMFHttp
from TapClientCore.T05_TM_feature.v2.TMF04_meta_transformer.tmf_meta_transformer import TMFMetaTransformerItems
from TapUtil.logfunc.logger import logger
from TapClientCore.T01_init_conf.v2_init.init_config import MFA, config, inspects_collection
from TapClientCore.T05_TM_feature.v2.TMF05_inspects.tmf_inspects import TMFInspects
from TapException.TM import *
from TapClientCore.T02_tm_api.v2_api.v2_tm import v2tm


class TPFInspectsBASE:
    TMF_INSPECTS = TMFInspects()
    TMF_META = TMFMetaTransformerItems()
    TMF_DATASOURCE = TMFHttp()
    serve = config.get('serve')
    user = v2tm.user

    def inspects_insert_mongodb(self, kwargs):
        MFA.insert_one(inspects_collection,
                       {
                           'serve': self.serve,
                           'user': self.user,
                           **kwargs
                       })

    def inspects_delete_mongodb(self, kwargs):
        name = kwargs.get('name')
        MFA.delete_one(inspects_collection,
                       {
                           'name': name,
                           'user': self.user,
                           'serve': self.serve
                       })

    def inspects_update_mongodb(self, kwargs):
        name = kwargs.get('name')
        return MFA.update(inspects_collection,
                          {'name': name,
                           'serve': self.serve,
                           'user': self.user,
                           },
                          {'$set': {**kwargs}})

    def inspects_query_mongodb(self, kwargs):
        """
        通过名字查询任务
        :param kwargs:
        :return:
        """
        name = kwargs.get('name')
        query_result = MFA.query(inspects_collection,
                                 {
                                     'name': name,
                                     'serve': self.serve,
                                     'user': self.user,
                                 })
        if query_result:
            return query_result[0]
        return query_result
